package cn.yuanqiao.archive.webservice.service.impl;/**
 * @author 赵常飞
 * @date 2024/3/28 11:45
 */

import cn.yuanqiao.archive.danganzhengli.domain.HandOverDa;
import cn.yuanqiao.archive.danganzhengli.domain.HandOverReceive;
import cn.yuanqiao.archive.danganzhengli.mapper.HandOverDaMapper;
import cn.yuanqiao.archive.danganzhengli.mapper.HandOverReceiveMapper;
import cn.yuanqiao.archive.danganzhengli.service.impl.HandOverReceiveServiceImpl;
import cn.yuanqiao.archive.webservice.service.receiveFeedbackWebService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.jws.WebService;

/**
 *@Author: 赵常飞
 *@Date: 2024/3/28 11:45
 *@Description:
 *@Note:
 */

@Service
@WebService(serviceName  = "receiveFeedback",targetNamespace = "http://service.webservice.archive.yuanqiao.cn",endpointInterface = "cn.yuanqiao.archive.webservice.service.receiveFeedbackWebService")
public class receiveFeedbackWebServiceImpl implements receiveFeedbackWebService {
    private final Logger logger = LoggerFactory.getLogger(receiveFeedbackWebServiceImpl.class);
    @Autowired
    private HandOverReceiveMapper handOverReceiveMapper;
    @Autowired
    private HandOverReceiveServiceImpl handOverReceiveService;
    @Autowired
    private HandOverDaMapper handOverDaMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String receiveFeedback(String feedbackInfo) {
        //jsstate 1 接收 -1退回
        logger.info("feedbackInfo={}",feedbackInfo);
        JSONArray array=JSONArray.parseArray(feedbackInfo);
        for (int i = 0; i < array.size(); i++) {
            JSONObject json=array.getJSONObject(i);
            String id=json.getString("IDENTIFIER");
            String msg=json.getString("RESULT");
            String tenantCode=json.getString("tenantCode");
            Integer state=-1;
            String feedback="";
            if("success".equals(msg)){ //state改为1
                state=1;
                feedback="已接收";
            }else if("fail".equals(msg)){ //state改为-1
                state=-1;
                feedback="退回";
            }else{
                state=6;
                feedback=msg;
            }
            HandOverReceive receiveUpdate = new HandOverReceive();
            receiveUpdate.setTableName(tenantCode+"_HAND_OVER_RECEIVE");
            receiveUpdate.setId(Long.parseLong(id));
            receiveUpdate.setJsState(state);
            receiveUpdate.setFeedback(feedback);
            handOverReceiveMapper.updateHandOverReceive(receiveUpdate);
            //接收单/接收单档案状态的修改
            handOverReceiveService.jsState(Long.parseLong(id),tenantCode);
            if(state==-1){
                HandOverDa handOverDa = new HandOverDa();
                handOverDa.setTableName(tenantCode+"_HAND_OVER_DA");
                handOverDa.setState(99);  //已接收
                handOverDa.setHandOverId(Long.parseLong(id));
                handOverDaMapper.updateHandOverByReId(handOverDa);
            }
        }
        return null;
    }
}
